<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 10/13/14
 * Time: 6:22 PM
 */
namespace BC\Games\Holdem\Hands;

class PokerHands
{
	/**
	 * Check an array of cards for any known poker hands
	 * @param array $cards
	 * @return IPokerHand[]
	 */
	static function getAllHands(Array $cards) {
		if(sizeof($cards) < 2)
			throw new \InvalidArgumentException("Invalid card array");
		/** @var IPokerHand $TwoPair */
		$Hands = array();

		FourOfAKindHand::match($cards, $Hands);
		StraightFlushHand::match($cards, $Hands);
		FlushHand::match($cards, $Hands);
		StraightHand::match($cards, $Hands);
		ThreeOfAKindHand::match($cards, $Hands);
		TwoPairHand::match($cards, $Hands);
		OnePairHand::match($cards, $Hands);

		if(sizeof($Hands) === 0)
			$Hands[] = new NoHand($cards);

		usort($Hands,
			function(IPokerHand $Hand1, IPokerHand $Hand2) {
				return $Hand2->compare($Hand1);
			}
		);

		return $Hands;
	}

	/**
	 * compare two sets of cards based on their hand[s] value
	 * @param array $cards1
	 * @param array $cards2
	 * @return int
	 */
	static function compareHands(Array $cards1, Array $cards2) {
		$Hands1 = self::getAllHands($cards1);
		$Hands2 = self::getAllHands($cards2);
			if(sizeof($Hands1) === 0)
				return -1;
			if(sizeof($Hands2) === 0)
				return 1;
		$compare = 0;
		foreach($Hands1 as $Hand1) {
			foreach ($Hands2 as $Hand2) {
				$c = $Hand1->compare($Hand2);
				if(abs($c) > abs($compare))
					$compare = $c;
			}
		}
		return $compare;
	}
}